package store.control;

import store.Index;
import store.Service.SelfInfomation;
import store.entry.User;
import java.util.ArrayList;
import java.util.Scanner;

public class UserService {
    public static ArrayList<User> userArr = new ArrayList<>();
    static Scanner sc = new Scanner(System.in);

    //登录
    public static void login() {
        System.out.println("请输入用户名");
        String username = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        if (userArr.size() != 0) {
            boolean flag = false;
            for (User user : userArr) {
                if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
                    System.out.println("您已登录成功！尊敬的" + username + "您好!");
                    flag = true;
                    Index.Indexstore();
                    break;
                }
            }
            if (!flag){
                System.out.println("用户名或密码错误");
                login();
            }
        } else {
            System.out.println("没有该用户，请先注册");
        }
    }

    //用户名判断
    public static boolean nameRight(String username) {
        boolean flag = false;
        if (username.equals("")) {
            System.out.println("用户名不能为空");
            System.out.println("请重新输入用户名");
            flag = true;

        }
        for (User user : userArr) {
            if (user.getUsername().equals(username)) {
                System.out.println("用户名已存在");
                flag = true;
            }
        }
        return flag;
    }

    //密码判断
    public static boolean pwdRight(String pwd) {
        boolean flag = false;
        if (pwd.equals("")) {
            System.out.println("密码不能为空");
            System.out.println("请重新输入密码");
            return flag;
        }
        if (pwd.length() < 6) {
            System.out.println("密码长度不能少于6位");
            System.out.println("请重新输入密码");
            return flag;
        }
        //二次输入密码
        //如果密码和上一次输入不一样，则提示密码不一致，请重新输入
        System.out.println("请再次输入密码：");
        String pwd2 = sc.next();
        while (!pwd.equals(pwd2)) {
            System.out.println("两次密码不一致，请重新输入");
            return true;
        }
        return flag;
    }

    //注册
    public static void register() {
        String username;
        String password;
        String otherName;
        int age;
        do {
            System.out.println("请输入用户名：");
            username = sc.next();
        } while (nameRight(username));
        do {
            System.out.println("请输入密码：");
            password = sc.next();
        } while (pwdRight(password));
        System.out.println("请输入年龄：");
        age = sc.nextInt();
        while (!ageRight(age)){
            System.out.println("您输入的年龄必须是0-100之间的数字");
            System.out.println("请重新输入年龄");
            age = sc.nextInt();
        }
        System.out.println("请输入昵称：");
        otherName = sc.next();
        System.out.println("注册成功");
        userArr.add(new User(username, password,otherName,age));
        for (User user : userArr) {
            if (SelfInfomation.infoArr.size() == 0){//判断infoArr个人信息是否为空，为空则直接存入
                SelfInfomation.infoArr.add(new User(username, password,user.getOtherName(),user.getAge()));
                break;
            }else {
                //修改 覆盖
                SelfInfomation.infoArr.clear();//不为空则清空，再添加
                SelfInfomation.infoArr.add(new User(username, password,user.getOtherName(),user.getAge()));
                break;
            }
        }
    }
    //年龄判断
    public static boolean ageRight(int age) {
        if (age < 0 || age > 100){
            return false;
        }
        return true;
    }
}
